-- Genero un nuevo bloque
-- dofile(minetest.get_modpath(ESTEMOD) .. "models/silexGenerator.lua")

--[[
minetest.register_node("oscodermod:horno", {
   description = "Horno para hacer silex",
   drawtype = "normal",
   tiles = {
       "silexGen.png"
   },
   
 })

-- Receta de concinar grava en silex.
minetest.register_craft({
    type = "cooking",
    output = "default:flint",
    recipe = "default:gravel",
    cooktime = 15.0
})

--
-- Formspecs
-- Copiados de furnace.
--

function default.get_furnace_active_formspec(fuel_percent, item_percent)
	return "size[8,8.5]"..
		"list[context;src;2.75,0.5;1,1;]"..
		"list[context;fuel;2.75,2.5;1,1;]"..
		"image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
		(fuel_percent)..":default_furnace_fire_fg.png]"..
		"image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
		(item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
		"list[context;dst;4.75,0.96;2,2;]"..
		"list[current_player;main;0,4.25;8,1;]"..
		"list[current_player;main;0,5.5;8,3;8]"..
		"listring[context;dst]"..
		"listring[current_player;main]"..
		"listring[context;src]"..
		"listring[current_player;main]"..
		"listring[context;fuel]"..
		"listring[current_player;main]"..
		default.get_hotbar_bg(0, 4.25)
end

function default.get_furnace_inactive_formspec()
	return "size[8,8.5]"..
		"list[context;src;2.75,0.5;1,1;]"..
		"list[context;fuel;2.75,2.5;1,1;]"..
		"image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
		"image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
		"list[context;dst;4.75,0.96;2,2;]"..
		"list[current_player;main;0,4.25;8,1;]"..
		"list[current_player;main;0,5.5;8,3;8]"..
		"listring[context;dst]"..
		"listring[current_player;main]"..
		"listring[context;src]"..
		"listring[current_player;main]"..
		"listring[context;fuel]"..
		"listring[current_player;main]"..
		default.get_hotbar_bg(0, 4.25)
end

]]--

--
-- Node callback functions that are the same for active and inactive furnace
--

local function swap_node(pos, name)
	local node = minetest.get_node(pos)
	if node.name == name then
		return
	end
	node.name = name
	minetest.swap_node(pos, node)
end

local function can_dig(pos, player)
	local meta = minetest.get_meta(pos);
	local inv = meta:get_inventory()
	return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
end

--
-- Node definitions
--

-- EJEMPLO DEL BARRELS
--

--[[

-- Wine barrel node
minetest.register_node("wine:wine_barrel", {
	description = S("Fermenting Barrel"),
	tiles = {"wine_barrel.png" },
	drawtype = "mesh",
	mesh = "wine_barrel.obj",
	paramtype = "light",
	paramtype2 = "facedir",
	groups = {
		choppy = 2, oddly_breakable_by_hand = 1, flammable = 2,
		tubedevice = 1, tubedevice_receiver = 1, axey = 1
	},
	legacy_facedir_simple = true,

	on_place = minetest.rotate_node,

	on_construct = function(pos)

		local meta = minetest.get_meta(pos)

		meta:set_string("formspec", winebarrel_formspec(0, "", 0))
		meta:set_string("infotext", S("Fermenting Barrel"))
		meta:set_float("status", 0)

		local inv = meta:get_inventory()

		inv:set_size("src", 4) -- ingredients
		inv:set_size("src_b", 1) -- water bucket
		inv:set_size("dst", 1) -- brewed item
	end,

	-- punch old barrel to change to new 4x slot variant and add a little water
	on_punch = function(pos, node, puncher, pointed_thing)

		local meta = minetest.get_meta(pos)
		local inv = meta and meta:get_inventory()
		local size = inv and inv:get_size("src")

		if size and size < 4 then

			inv:set_size("src", 4)
			inv:set_size("src_b", 1)

			meta:set_int("water", 50)
			meta:set_string("formspec", winebarrel_formspec(0, "", 50))
		end
	end,

	can_dig = function(pos,player)

		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()

		if not inv:is_empty("dst")
		or not inv:is_empty("src")
		or not inv:is_empty("src_b") then
			return false
		end

		return true
	end,

	allow_metadata_inventory_take = function(pos, listname, index, stack, player)

		if minetest.is_protected(pos, player:get_player_name()) then
			return 0
		end

		return stack:get_count()
	end,

	allow_metadata_inventory_put = function(pos, listname, index, stack, player)

		if minetest.is_protected(pos, player:get_player_name()) then
			return 0
		end

		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()

		if listname == "src" then

			return stack:get_count()

		elseif listname == "src_b" then

			local water = meta:get_int("water")

			-- water full, return item
			if water == 100 then
				return 0
			end

			local is_bucket = stack:get_name()
			local is_water = water_check(is_bucket)

			if is_water then
				return stack:get_count()
			else
				return 0
			end

		elseif listname == "dst" then

			return 0
		end
	end,

	allow_metadata_inventory_move = function(
			pos, from_list, from_index, to_list, to_index, count, player)

		if minetest.is_protected(pos, player:get_player_name()) then
			return 0
		end

		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		local stack = inv:get_stack(from_list, from_index)

		if to_list == "src" then
			return count

		elseif to_list == "dst" then
			return 0

		elseif to_list == "src_b" then
			return 0
		end
	end,

	on_metadata_inventory_put = function(pos, listname, index, stack, player)

		if listname == "src_b" then

			local meta = minetest.get_meta(pos)
			local inv = meta:get_inventory()
			local is_bucket = inv:get_stack("src_b", 1):get_name()
			local is_water = water_check(is_bucket)

			if is_water then

				local water = meta:get_int("water")
				local amount = tonumber(is_water[3]) or 0

				water = water + amount

				if water > 100 then water = 100 end

				inv:remove_item("src_b", is_water[1])
				inv:add_item("src_b", is_water[2])

				local status = meta:get_float("status")

				meta:set_int("water", water)
				meta:set_string("formspec",
						winebarrel_formspec(status, S("Water Added"), water))
			end
		end

		local timer = minetest.get_node_timer(pos)

		if not timer:is_started() then
			minetest.get_node_timer(pos):start(5)
		end
	end,

	on_metadata_inventory_move = function(pos)

		local timer = minetest.get_node_timer(pos)

		if not timer:is_started() then
			minetest.get_node_timer(pos):start(5)
		end
	end,

	on_metadata_inventory_take = function(pos)

		local timer = minetest.get_node_timer(pos)

		if not timer:is_started() then
			minetest.get_node_timer(pos):start(5)
		end
	end,

	pipe_connections = {
		left = 1, right = 1, front = 1, back = 1,
		left_param2 = 3, right_param2 = 1, front_param2 = 2, back_param2 = 0
	},

	after_dig_node = function(pos)

		if pipe then
			pipeworks.scan_for_pipe_objects(pos)
		end
	end,

	tube = (function() if pipe then return {

		-- using a different stack from defaut when inserting
		insert_object = function(pos, node, stack, direction)

			local meta = minetest.get_meta(pos)
			local inv = meta:get_inventory()
			local timer = minetest.get_node_timer(pos)

			if not timer:is_started() then
				timer:start(5)
			end

			return inv:add_item("src", stack)
		end,

		can_insert = function(pos, node, stack, direction)

			local meta = minetest.get_meta(pos)
			local inv = meta:get_inventory()

			return inv:room_for_item("src", stack)
		end,

		-- the default stack, from which objects will be taken
		input_inventory = "dst",

		connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}

	} end end)(),

	on_timer = function(pos)

		local meta = minetest.get_meta(pos) ; if not meta then return end
		local inv = meta:get_inventory()
		local water = meta:get_int("water") or 0

		-- check for pipeworks water inlet
		if pipe then

			if minetest.find_node_near(pos, 1, pipeworks.pipes_full_nodenames) then

				water = water + 20

				if water > 100 then water = 100 end

				meta:set_int("water", water)
			end
		end

		-- is barrel empty?
		if not inv or inv:is_empty("src") then

			meta:set_float("status", 0)
			meta:set_string("infotext", S("Fermenting Barrel"))
			meta:set_string("formspec", winebarrel_formspec(0, "", water))

			return false
		end

		-- check water level
		if water < 5 then

			txt = S("Fermenting Barrel") .. " " .. S("(Water Level Low)")

			meta:set_string("infotext", txt)
			meta:set_float("status", 0)
			meta:set_string("formspec", winebarrel_formspec(0,
					S("(Water Level Low)"), water))

			return false
		end

		-- does it contain any of the source items on the list?
		local has_items, recipe, item1, item2, item3, item4

		for n = 1, #ferment do

			recipe = ferment[n]

			item1 = recipe[1][1] and ItemStack(recipe[1][1])
			item2 = recipe[1][2] and ItemStack(recipe[1][2])
			item3 = recipe[1][3] and ItemStack(recipe[1][3])
			item4 = recipe[1][4] and ItemStack(recipe[1][4])

			-- check for recipe items
			if item1 then

				has_items = inv:contains_item("src", item1)

				if has_items and item2 then

					has_items = inv:contains_item("src", item2)

					if has_items and item3 then

						has_items = inv:contains_item("src", item3)

						if has_items and item4 then
							has_items =  inv:contains_item("src", item4)
						end
					end
				end
			end

			-- if we have all items in recipe break and continue
			if has_items then
				break
			end
		end

		-- if we have a wrong recipe change status
		if not has_items then

			txt = S("Fermenting Barrel") .. " " .. S("(No Valid Recipe)")

			meta:set_string("infotext", txt)
			meta:set_float("status", 0)
			meta:set_string("formspec",
					winebarrel_formspec(0, S("(No Valid Recipe)"), water))

			return false
		end

		-- is there room for additional fermentation?
		if not inv:room_for_item("dst", recipe[2]) then

			txt = S("Fermenting Barrel") .. " " .. S("(Output Full)")

			meta:set_string("infotext", txt)
			meta:set_string("formspec",
					winebarrel_formspec(0, S("(Output Full)"), water))

			return false
		end

		local status = meta:get_float("status")

		-- fermenting (change status)
		if status < 100 then

			txt = S("Fermenting Barrel") .. " " .. S("(@1% Done)", status)

			meta:set_string("infotext", txt)
			meta:set_float("status", status + 5)
]]--
	--		local desc = minetest.registered_items[recipe[2]].description or ""
--[[
			txt = S("Brewing: @1", desc) .. " " .. S("(@1% Done)", status)

			meta:set_string("formspec", winebarrel_formspec(status, txt, water))

		else -- when we hit 100% remove items needed and add beverage

			if item1 then inv:remove_item("src", item1) end
			if item2 then inv:remove_item("src", item2) end
			if item3 then inv:remove_item("src", item3) end
			if item4 then inv:remove_item("src", item4) end

			inv:add_item("dst", recipe[2])

			water = water - 5

			meta:set_float("status", 0)
			meta:set_int("water", water)
			meta:set_string("formspec", winebarrel_formspec(0, "", water))
		end

		if inv:is_empty("src") then
			meta:set_float("status", 0.0)
			meta:set_string("infotext", S("Fermenting Barrel"))
		end

		return true
	end
})

]]--



-- CUBO HORNO (HAY 2 porque cambia el cubo según el estado)
minetest.register_node("oscodermod:horno", {
} )

-- CUBO HORNO ACTIVO
minetest.register_node("oscodermod:horno_active", {
    description = "Horno para hacer silex en estado activo",
    tiles = {
        "silexGen_active.png"
    },
    on_punch = function (pos)
        swap_node(pos, "oscodermod:horno")
    end
} )

minetest.register_node("oscodermod:horno", {
    description = "Horno para hacer silex",
    tiles = {
        "silexGen.png"
    },
    on_punch = function (pos)
        swap_node(pos, "oscodermod:horno_active")
    end
    
} )

-- RECETA PARA CREAR EL "NUEVO HORNO"
minetest.register_craft({
	output = "oscodermod:horno",
	recipe = {
		{"", "", ""},
		{"default:cobblestone", "default:gravel", "default:cobblestone"},
		{"default:stone", "default:stone", "default:stone"},
	}
})



